//index.js
import alert from '../../utils/alert.js';
import http from '../../utils/request.js';
//获取应用实例
const app = getApp()
Page({
  data: {
    gridMenus: [],
    indicatorDots: true,
    autoplay: true,
    circular: true,
    interval: 2000,
    poi: [],
    menus: []
  },
  onLoad: function (options) {
    this.storeInit();
  },
  /**
 * 生命周期函数--监听页面初次渲染完成
 */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (options) {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    var memberInfo = wx.getStorageSync('userInfo').memberInfo;
    return {
      title: '点餐小程序',
      path: 'pages/index/index?form_id=' + wx.getStorageSync('userInfo').memberInfo.openid,
      success: function (res) {
        res.shareTickets // 单聊是没有的
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },
  
  storeInit:function() {
    http.get(
      app.config.service.getStoreInfo,
    ).then(data => {
     this.setData({
       poi: data
     })
    }, err => {
      alert.alert(err)
    });
  },
  
  scanTableQr: function (e) {
    var that = this;
    var scanOptions = {
      onlyFromCamera: true,
      scanType: 'qrCode',
      success(res) {
        console.log(res)
        if (res.scanType == "WX_CODE") {
          console.log(res.path)
          var table_id = http.urlparams(res.path, 'table_id')
          var mode = http.urlparams(res.path, 'mode')
          if (mode == "scan" && table_id !== undefined) {
            that.getOrderByTable(table_id)
          } else {
            that.storeInit()
          }
        }
      },
      fail(error) {
        alert.alert(error.errMsg)
      }
    };
    wx.scanCode(scanOptions)
  },

  scanPayQr:function(e){
    var that = this;
    var scanOptions = {
      onlyFromCamera: true,
      scanType: 'qrCode',
      success(res) {
        console.log(res)
        if (res.scanType == "WX_CODE") {
          var table_id = http.urlparams(res.path, 'table_id')
          var mode = http.urlparams(res.path, 'mode')
          if (mode == "scan" && table_id !== undefined) {
            that.getOrderByTable(table_id)
          }else{
            alert.alert('二维码无效');
          }
        }
      },
      fail(error) {
        alert.alert(error.errMsg)
      }
    };
    wx.scanCode(scanOptions)
  },

  goTable: function (table_id){
    wx.navigateTo({
      url: '../goods/index?table_id=' + table_id,
    })
  },

  callPhone: function (e) {
    wx.makePhoneCall({ phoneNumber: e.currentTarget.dataset.phone });
  },

  goMap: function (e) {
    var that = this;
    console.log(that.data.poi);
    wx.openLocation({
      name: that.data.poi.map_address + that.data.poi.name,
      latitude: parseFloat(that.data.poi.gcj02_map[1]),
      longitude: parseFloat(that.data.poi.gcj02_map[0]),
      scale: 15,
      address: that.data.poi.address
    })
  },

  getOrderByTable: function (table) {
    http.post(
      app.config.service.getOrderByTable,
      {table_id: table}
    ).then(data => {
      if (!data.order) {
        wx.navigateTo({
          url: '../goods/index?table_id=' + table,
        })
      } else {
        wx.navigateTo({
          url: '../order/info/index?order_no=' + data.order.order_no,
        })
      }
    }, err => {
      console.log(err)
    });
  },
  connectWifi:function(e){
    wx.startWifi({
      success(res) {
        console.log(res.errMsg)
      }
    })

    wx.getConnectedWifi(
      {
        success(res) {
          if (res.wifi.SSID == e.currentTarget.dataset.ssid){
            alert.toast('您已经连接上此WIFI了');
            return;
          }
        },
        error(res) {
          console.log(res)
        },
        complete(res){
          console.log(res)
        }
      }
    )
    wx.connectWifi({
      SSID: e.currentTarget.dataset.ssid,
      password: e.currentTarget.dataset.pwd,
      success(res) {
        console.log(res.errMsg)
      },
      error(res){
        console.log(res)
      },
      complete(res){
        if (res.errCode==0){
          alert.toast('您已经连接上此WIFI了');
        }else{
          alert.alert(res.errMsg);
        }
      }
    })
  }
  
})
